<!DOCTYPE html>
<html lang="en">
<head>
    <title>角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css"/>
</head>
<body>
<!--搜索框-->
<div class="demoTable">
    名称:
    <div class="layui-inline">
        <input class="layui-input" name="name" id="name" autocomplete="off">
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>
<table class="layui-hide" id="Role2" lay-filter="Role2"></table>
<!-- 自定义的控件，表格左上角位置按钮 -->
<div id="toolbar" style="display: none; ">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add" data-method="notice" data-type="auto">添加</button>
    </div>
</div>
<!--操作列表按钮-->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--添加修改表单，通过拟态框来见实现-->
<div id="modal" style="display:none">
    <form class="layui-form" lay-filter="entity">
        <input type="text" name="id" class="layui-input" style="display:none">
        <div class="layui-form-item">
            <label class="layui-form-label">显示顺序:</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="roleSort" required lay-verify="required" placeholder="显示顺序"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色标识:</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="role" required lay-verify="required" placeholder="角色标识"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="remark" required lay-verify="required" placeholder="备注"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <input type="radio" name="status" value="0" title="显示"
                       checked=""><div class="layui-unselect layui-form-radio"><i class="layui-anim
                       layui-icon"> </i><div>显示</div></div>
                <input type="radio" name="status" value="1" title="隐藏"><div
                    class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim
                    layui-icon"> </i><div>隐藏</div></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">删除标志</label>
            <div class="layui-input-inline">
                <input type="radio" name="delFlag" value="0" title="存在"
                       checked=""><div class="layui-unselect layui-form-radio"><i class="layui-anim
                       layui-icon"> </i><div>存在</div></div>
                <input type="radio" name="delFlag" value="2" title="删除"><div
                    class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim
                    layui-icon"> </i><div>删除</div></div>
                <input type="radio" name="delFlag" value="1" title="管理员"><div
                    class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim
                    layui-icon"> </i><div>管理员</div></div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button style="display: none;" type="reset" class="layui-btn layui-btn-primary swiper-right-form-reset">重置</button>
            </div>
        </div>
    </form>
</div>

<script src="/layui/layui.js" charset="utf-8"></script>

<script>
    layui.use(['table', 'layer', 'form', 'jquery'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        table.render({
            elem: '#Role2'
            , url: '/api/rolew/page'
            , page: true
            , id: 'testReload'
            , cols: [[
                {field: "id", title: "ID"},
                {field: "roleSort", title: "显示顺序"},
                {field: "role", title: "角色标识"},
                {field: "remark", title: "备注"},
                {field: "status", title: "状态", templet: function (d) {
                        if(d.status==='0'){
                            return '正常';
                        }
                        if(d.status==='1'){
                            return '停用';
                        }
                    }},
                {field: "delFlag", title: "删除状态",templet: function (d) {
                        if(d.delFlag==='0'){
                            return '存在';
                        }
                        if(d.delFlag==='1'){
                            return '管理员';
                        }
                        if(d.delFlag==='2'){
                            return '删除';
                        }
                    }},
                {
                    field: "createDatetime",
                    title: "创建时间",
                    templet: "<div>{{layui.util.toDateString(d.createTime,'yyyy-MM-dd HH:mm:ss')}}</div>"
                },
                {title: '操作', width: 150, toolbar: '#barDemo', align: 'center'}
            ]]
            , toolbar: '#toolbar',
            response: {
                statusCode: 1
            },
            parseData: function (rs) {//数据格式解析
                if (rs.status == 1) {
                    return {
                        "code": rs.status,
                        "msg": rs.message,
                        "count": rs.total,
                        "data": rs.data
                    }
                }
            }
        });
        //监听事件
        table.on('toolbar(Role2)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    open2("添加页面", null);
                    break;
            };
        });
        //监听工具条
        table.on('tool(Role2)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除这条信息吗？', function (index) {
                    $.ajax({
                        type: "delete",
                        url: "/api/rolew/" + data.id,
                        dataType: "json",
                        success: function (da) {
                            if (da.status == 1) {
                                layer.msg(da.message)
                                obj.del();
                            } else {
                                layer.msg(da.message)
                            }
                        },
                        error: function (da) {
                            alert("出现错误");
                            return false;
                        }
                    }) //ajax结束
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                open2("修改页面", obj.data);
            }
        });

        //"模态框",
        function open2(title, entity) {
            form.val("entity", entity);
            layer.open({
                type: 1
                , title: title
                , content: $("#modal")
                , offset: '10px'
                , area: ['600px', '80%']
                , end: function () {
                    $('.swiper-right-form-reset').click()
                    table.reload('testReload');
                }
            });
        }
        //监听提交
        form.on('submit(formDemo)', function (data) {
            var data1 = form.val("entity");
            console.log(data1);
            var type = data1.id ? "put" : "post";
            console.log(type);
            $.ajax({
                url: '/api/rolew',
                type: type,
                dataType: "json",
                contentType: 'application/json',
                data: JSON.stringify(data1),
                success: function (da) {
                    if (da.status == 1) {
                        layer.msg(da.message,{time:1*1000},function() {
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(da.message);
                    }
                }
            });
            return false;
        });

        var $ = layui.$, active = {
            reload: function () {
                var name = $('#name');
                table.reload('testReload', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        "remark": name.val()
                    }
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>